package com.aierkeji.call.b2c.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aierkeji.call.b2c.dao.jpa.AkjCallVersionDao;
import com.aierkeji.call.b2c.entity.AkjCallVersion;
import com.aierkeji.call.b2c.service.AkjCallClueService;
import com.aierkeji.call.b2c.service.AkjCallLoginService;
import com.aierkeji.call.b2c.service.AkjCallVersionService;
import com.aierkeji.call.b2c.util.ResultMapHelper;

@RestController
@RequestMapping("login")
public class AkjCallLoginController extends BaseController {
	private final Logger log = LogManager.getLogger(getClass());

	@Autowired
	HttpServletRequest httpServletRequest;
	@Autowired
	AkjCallLoginService akjCallLoginService;

	@Autowired
	AkjCallVersionService akjCallVersionService;
	
	@Autowired
	private AkjCallClueService akjCallClueService;

	/**
	 * App打开先调这个接口获取token
	 * 
	 * @return
	 */
	@GetMapping("getSessionId")
	public Map<String, Object> getSessionId() {
		return ResultMapHelper.success(SecurityUtils.getSubject().getSession().getId());
	}

	@PostMapping("registered")
	public Map<String, Object> registered(@RequestBody Map<String, Object> map) {

		return akjCallLoginService.registered(map);
	}

	/**
	 * 所有获取验证码都是调这个接口 传入scene 来区分场景
	 * 
	 * @param map
	 * @return
	 */
	@PostMapping("getYZM")
	public Map<String, Object> getYZM(@RequestBody Map<String, Object> map) {

		return akjCallLoginService.getYZM(map);
	}

	@PostMapping("loginByYzm")
	public Map<String, Object> loginByYzm(@RequestBody Map<String, Object> map, HttpServletRequest request) {
		return akjCallLoginService.loginByYzm(map, request);
	}

	@PostMapping("login")
	public Map<String, Object> login(@RequestBody Map<String, Object> map, HttpServletRequest request) {

		return akjCallLoginService.loginByPassword(map, request);
	}

	@PostMapping("getUserInfo")
	public Map<String, Object> getUserInfo(@RequestBody Map<String, Object> map) {
		return akjCallLoginService.getUserInfo(map);
	}

	@PostMapping("updateUserInfo")
	public Map<String, Object> updateUserInfo(@RequestBody Map<String, Object> map) {
		return akjCallLoginService.updateUserInfo(map);
	}

	@GetMapping("logout")
	public Map<String, Object> logout(HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return ResultMapHelper.success();
	}

	@GetMapping("getVersion")
	public Map<String, Object> getVersion() {
		AkjCallVersion akjCallVersion = akjCallVersionService.getVersion();
		return ResultMapHelper.success(akjCallVersion);
	}

}
